//
//  DiagnosisCompletedViewController.m
//  MobileChecker
//
//  Created by Lymons on 15-1-13.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "DiagnosisCompletedViewController.h"
#import "DiagnosisResultViewController.h"
#import "CheckResultItemViewModel.h"
#import "ViewController.h"

@interface DiagnosisCompletedViewController ()

@property (strong, nonatomic) IBOutlet UILabel *diagnosisResult;
@property (weak, nonatomic) IBOutlet UIButton *imeiInputButton;
@property (weak, nonatomic) IBOutlet UIButton *priceButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *returnWithWhoConstraint;
@property (weak, nonatomic) IBOutlet UIButton *returnToFirstButton;

@property (strong, nonatomic) CheckResultItemViewModel *viewModel;
@property (strong, nonatomic) NSLayoutConstraint *returnWithPriceConstraint;

@end

@implementation DiagnosisCompletedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self bindViewModel];
    
    self.imeiInputButton.hidden = ![CheckResultUserDefaults standardUserDefaults].shouldShowIMEIButton;
    self.priceButton.hidden = ![CheckResultUserDefaults standardUserDefaults].shouldShowPriceButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSLayoutConstraint *)returnWithPriceConstraint {
    if (_returnWithPriceConstraint == nil) {
        _returnWithPriceConstraint = [NSLayoutConstraint constraintWithItem:self.returnToFirstButton
                                                                 attribute:NSLayoutAttributeBottom
                                                                 relatedBy:NSLayoutRelationEqual
                                                                    toItem:self.priceButton
                                                                 attribute:NSLayoutAttributeTop
                                                                multiplier:1.0
                                                                  constant:-10];
    }
    return _returnWithPriceConstraint;
}

- (void)bindViewModel
{
    self.viewModel = [[CheckResultItemViewModel alloc] init];
    RAC(self, diagnosisResult.text) = RACObserve(self, viewModel.resultString);
    RAC(self, diagnosisResult.textColor) = [RACObserve(self, diagnosisResult.text) map:^id(NSString *value) {
        return [CheckResultUserDefaults valueColor:value];
    }];
    
    @weakify(self);
    [RACObserve(self, imeiInputButton.hidden) subscribeNext:^(NSNumber *value) {
        @strongify(self);
        [self updateReturnButtonConstraints:[value boolValue]];
    }];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"DiagnosisPresent"]) {
        if ([segue.destinationViewController isKindOfClass:[DiagnosisResultViewController class]]) {
            DiagnosisResultViewController *vc = segue.destinationViewController;
            vc.hideBottomButton = YES;
        }
    } else if ([segue.identifier isEqualToString:@"SendResult"]) {
        if ([segue.destinationViewController isKindOfClass:[ViewController class]]) {
            ViewController *vc = segue.destinationViewController;
            vc.sendingResult = YES;
        }
    }
}


- (IBAction)unwindSegueToCompletedViewController:(UIStoryboardSegue *)segue {
    
}

#pragma mark - Private 

- (void)updateReturnButtonConstraints:(BOOL)hidden {
    if (hidden) {
        [self.view removeConstraint:self.returnWithWhoConstraint];
        [self.view addConstraint:self.returnWithPriceConstraint];
    } else {
        [self.view removeConstraint:self.returnWithPriceConstraint];
        [self.view addConstraint:self.returnWithWhoConstraint];
    }
}

@end
